<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');


/**
 * Initialize Messages that will be shown on the permission screen.
 * @access public 
 */
function init_permission()
{
    $ci = &get_instance();
    // initialize permission messages
    init_messages('Permission');
    
    // additional message
    $ci->messages['invalid_parameters'] = 'Invalid post data';
}

/**
 * Displays permissions list page.
 * @access public
 * @param array $content An array of permissions to be shown.
 * @param string $message The type of message to be shown.
 */
function view_all_permissions($message_type = NULL)
{
    $ci = &get_instance();

    // create breadcrumb
    $ci->breadcrumb->clear();
    $ci->breadcrumb->add_crumb('Permissions');
    
    $data = array();
    
    // set message
    $message                    = get_message($message_type);
    $data['message']            = $message;

    // active navigation
    $data['navigation'] = 'permissions';
    
    $companies                  = $ci->company_model->all()->get();
    $company_permissions        = array();
    
    foreach($companies as $company)
    {
        $company_modules                    = $ci->company_modules_model->column( array('company_id' => $company['id']), 'module_id' )->get();
        $modules                            = $ci->module_model->all_in( NULL, $company_modules )->get();
        
        $company_roles                      = $ci->company_roles_model->column( array('company_id' => $company['id']), 'group_id' )->get();
        $roles                              = $ci->role_model->all_in( NULL, $company_roles )->get();
        
        $company_permission['company']      = $company;
        $company_permission['modules']      = $modules;
        $company_permission['roles']        = $roles;
        
        $permissions                = array();

        foreach( $roles as $role )
        {
            foreach($modules as $module)
            {
                $company_id     = $company['id'];
                $group_id       = $role['id'];
                $module_id      = $module['id'];
//                    $args           = array( 'group_id' => $group_id, 'module_id' => $module_id);
                $args           = array( 'company_id' => $company_id, 'group_id' => $group_id, 'module_id' => $module_id);

//                    $permissions[$group_id][$module_id] = $ci->permission_model->get_one( $args );
                $permissions[$company_id][$group_id][$module_id] = $ci->permission_model->one($args)->get();
            }
        }
            
        $company_permission['permissions']   = $permissions;

        // set permissions keys
        $permission_set = array('View', 'Add', 'Edit', 'Delete');
        foreach($modules as $module)
        {
            $permissions = isset($module['permissions']) ? unserialize($module['permissions']) : array();
            foreach($permissions as $permission)
            {
                $permission = changeCase($permission, 'human_upper');
                ( ! in_array($permission, $permission_set) ) ? array_push($permission_set, $permission) : '';
            }
        }
        
        $company_permission['permission_set']           = $permission_set;
        $company_permissions[$company['id']]            = $company_permission;
    }

    $data['permissions'] = $company_permissions;
    
    // build breadcrumb
    $ci->breadcrumb->clear();
    $ci->breadcrumb->add_crumb('Permissions');

    // build template
    $ci->template
            ->title('Permissions')
            ->set_layout('main')
            ->set_partial('navigation', 'layouts/navigation')
            ->set_partial('footer', 'layouts/footer')
            ->set_partial('notifications', 'layouts/notifications')
//            ->set_partial('modal', 'layouts/modal')
            ->build('all_permissions', $data);
}

/**
 * Displays permissions list page.
 * @access public
 * @param array $content An array of permissions to be shown.
 * @param string $message The type of message to be shown.
 */
function view_permissions_list($message_type = NULL)
{
    $ci = &get_instance();

    // create breadcrumb
    $ci->breadcrumb->clear();
    $ci->breadcrumb->add_crumb('Permissions');
    
    $data = array();
    
    // set message
    $message                    = get_message($message_type);
    $data['message']            = $message;

    // active navigation
    $data['navigation'] = 'permissions';
    
    $modules                    = $ci->module_model->all()->get();
    $roles                      = $ci->role_model->all()->get();
    $data['roles']              = $roles;
    $data['modules']            = $modules;
    
    $permissions                = array();
    foreach( $roles as $role )
    {
        foreach($modules as $module)
        {
            $group_id       = $role['id'];
            $module_id      = $module['id'];
            $args           = array( 'group_id' => $group_id, 'module_id' => $module_id);
            
//            $permissions[$group_id][$module_id] = $ci->permission_model->get_attribute( $args , 'group_permissions');
            $permissions[$group_id][$module_id] = $ci->permission_model->one($args)->get();
        }
    }
    $data['permissions']        = $permissions;
    
    // set permissions keys
    $permission_set = array('View', 'Add', 'Edit', 'Delete');
    foreach($modules as $module)
    {
        $permissions = isset($module['permissions']) ? unserialize($module['permissions']) : array();
        foreach($permissions as $permission)
        {
            $permission = changeCase($permission, 'human_upper');
            ( ! in_array($permission, $permission_set) ) ? array_push($permission_set, $permission) : '';
        }
    }
    $data['permission_set']         = $permission_set;

    // build breadcrumb
    $ci->breadcrumb->clear();
    $ci->breadcrumb->add_crumb('Permissions');

    // build template
    $ci->template
            ->title('Permissions')
            ->set_layout('main')
            ->set_partial('navigation', 'layouts/navigation')
            ->set_partial('footer', 'layouts/footer')
            ->set_partial('notifications', 'layouts/notifications')
//            ->set_partial('modal', 'layouts/modal')
            ->build('role_permissions', $data);
}



/* End of file permission_helper.php */
/* Location: ./application/permissions/backend-permissions/helpers/permission_helper.php */